//
//  GTWeChatDynamicTopView.m
//  GTW
//
//  Created by Ran on 15/11/12.
//  Copyright (c) 2015年 xcode. All rights reserved.
//

#import "GTWeChatDynamicTopView.h"
#import "GTWeChatDynamicTool.h"
#import "GTWeChatDynamicSettings.h"
#import "RanTestModel.h"
#import "GTWeChatDynamicIconImgView.h"
#import "GTWeChatDynamicLinkButton.h"
#import "UIButton+RanSet.h"
#import "UIView+Ran.h"
#import "GTWeChatDynamicTuple.h"

#pragma mark - ------------------------Model------------------------

@interface GTWeChatDynamicTopViewModel: NSObject

@property(nonatomic, strong)RanTestModel *sourceModel;
@property(nonatomic, assign)CGFloat theWidth;

@property(nonatomic, assign)CGRect iconFrame;
@property(nonatomic, assign)CGRect nameFrame;
@property(nonatomic, assign)CGRect timeFrame;
@property(nonatomic, assign)CGRect locationFrame;

@end

@implementation GTWeChatDynamicTopViewModel

@end

#pragma mark - ------------------------View------------------------

@interface GTWeChatDynamicTopView()

@property(nonatomic, strong)GTWeChatDynamicIconImgView *iconImgView;
@property(nonatomic, strong)GTWeChatDynamicLinkButton *nameButton;
@property(nonatomic, strong)UILabel *timeLabel;
@property(nonatomic, strong)GTWeChatDynamicLinkButton *locationButton;

@property(nonatomic, strong)GTWeChatDynamicTopViewModel *model;

@end

@implementation GTWeChatDynamicTopView

const CGFloat spacingBetweenIconAndName = 10;
const CGFloat spacingBetweenTimeAndLocation = 10;

#pragma mark - System

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

#pragma mark - Private

- (void)setup
{

}

#pragma mark - Protocol

+ (GTWeChatDynamicTuple *)getProcessedDataFromSourceData: (id)sourceData withWidth: (CGFloat)theWidth
{
    GTWeChatDynamicTopViewModel *model = [[GTWeChatDynamicTopViewModel alloc] init];
    model.sourceModel = (RanTestModel *)sourceData;
    model.theWidth = theWidth;
    CGFloat height = 40;
    
    CGFloat iconWidth = 40;
    CGFloat iconHeight = iconWidth;
    CGFloat iconX = 0;
    CGFloat iconY = 0;
    model.iconFrame = CGRectMake(iconX, iconY, iconWidth, iconHeight);
    
    CGFloat nameX = CGRectGetMaxX(model.iconFrame) + spacingBetweenIconAndName;
    CGFloat nameY = 0;
    CGFloat nameWidth = theWidth - nameX;
    CGSize nameSize = [GTWeChatDynamicTool getTextSizeWithText:model.sourceModel.name attribute:[GTWeChatDynamicSettings userNameTextAttributes]];
    CGFloat nameHeight = nameSize.height;
    nameWidth = MIN(nameWidth, nameSize.width);
    model.nameFrame = CGRectMake(nameX, nameY, nameWidth, nameHeight);
    
    CGFloat timeHeight = [GTWeChatDynamicTool getTextHeightWithAttribute:[GTWeChatDynamicSettings timeTextAttributes]];
    CGFloat timeWidth = 0;
    CGFloat timeX = nameX;
    CGFloat timeY = height - timeHeight;
    model.timeFrame = CGRectMake(timeX, timeY, timeWidth, timeHeight);
    
    CGSize locationSize = [GTWeChatDynamicTool getTextSizeWithText:model.sourceModel.location attribute:[GTWeChatDynamicSettings locationTextAttributes]];
    CGFloat locationX = 0;
    CGFloat locationY = height - locationSize.height;
    CGFloat locationWidth = locationSize.width;
    CGFloat locationHeight = locationSize.height;
    model.locationFrame = CGRectMake(locationX, locationY, locationWidth, locationHeight);

    GTWeChatDynamicTuple *tuple = [[GTWeChatDynamicTuple alloc] init];
    tuple.data = model;
    tuple.height = height;
    return tuple;
}

#pragma mark - Event

- (void)tap
{

}

- (void)feedbackStoreData:(id)data
{
    self.model = (GTWeChatDynamicTopViewModel *)data;
    self.iconImgView.frame = self.model.iconFrame;
    self.iconImgView.image = [UIImage imageNamed:self.model.sourceModel.imageUrl];
    
    self.nameButton.frame = self.model.nameFrame;
    self.nameButton.text = self.model.sourceModel.name;
    
    NSString *time = self.model.sourceModel.timeStamp;
    self.timeLabel.frame = self.model.timeFrame;
    self.timeLabel.text = time;
    self.timeLabel.ranWidth =[GTWeChatDynamicTool getTextSizeWithText:time attribute:[GTWeChatDynamicSettings timeTextAttributes]].width;

    self.locationButton.frame = self.model.locationFrame;
    self.locationButton.ranX = CGRectGetMaxX(self.timeLabel.frame) + spacingBetweenTimeAndLocation;
    self.locationButton.ranWidth = MIN(self.locationButton.ranWidth, self.model.theWidth - self.locationButton.ranX);
    self.locationButton.text = self.model.sourceModel.location;
    
}
                        
#pragma mark - Getter
                        
- (GTWeChatDynamicIconImgView *)iconImgView
{
    if (!_iconImgView) {
        self.iconImgView = ({
            GTWeChatDynamicIconImgView *iconImgView = [[GTWeChatDynamicIconImgView alloc] init];
            [self addSubview:iconImgView];
            iconImgView;
        });
    }
    return _iconImgView;
}

- (GTWeChatDynamicLinkButton *)nameButton
{
    if (!_nameButton) {
        self.nameButton = ({
            GTWeChatDynamicLinkButton *nameButton = [GTWeChatDynamicTool linkButtonWithAttribute:[GTWeChatDynamicSettings userNameTextAttributes]];
            [self addSubview:nameButton];
            nameButton;
        });
    }
    return _nameButton;
}

- (UILabel *)timeLabel
{
    if (!_timeLabel) {
        self.timeLabel = ({
            UILabel *timeLabel = [GTWeChatDynamicTool labelWithAttribute:[GTWeChatDynamicSettings timeTextAttributes]];
            [self addSubview:timeLabel];
            timeLabel;
        });
    }
    return _timeLabel;
}

- (GTWeChatDynamicLinkButton *)locationButton
{
    if (!_locationButton) {
        self.locationButton = ({
            GTWeChatDynamicLinkButton *locationButton = [GTWeChatDynamicTool linkButtonWithAttribute:[GTWeChatDynamicSettings locationTextAttributes]];
            [self addSubview:locationButton];
            [locationButton addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside];
            locationButton;
        });
    }
    return _locationButton;
}

@end







